package zju.dgm.runner;

import lombok.Getter;
import lombok.Setter;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import zju.dgm.util.CacheManager;
import zju.dgm.util.Paths;
import zju.dgm.service.ServiceEntry;
import zju.dgm.util.SyncUtil;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.ThreadPoolExecutor;

@Component
@Setter
@Getter
public class MyRunner implements ApplicationRunner {

    @Autowired
    ApplicationContext ctx;

    @Resource
    CuratorFramework client;

    @Resource
    Paths paths;

    @Value("${server.port}")
    int serverPort;


    @Resource
    CacheManager cacheManager;




    private final Logger log = LoggerFactory.getLogger(getClass());

    @Override
    public void run(ApplicationArguments args) throws Exception {


        for (String daemonServiceName : ctx.getBeanNamesForType(ServiceEntry.class)) {
            ServiceEntry service = ctx.getBean(daemonServiceName, ServiceEntry.class);
            if (service.needRootPath()) {// 创建路径
                ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(), service.getRootPath());
            }
            //执行服务
            SyncUtil.tpe.submit(service);
        }


    }


}




